from fastapi import APIRouter, Depends, status
from sqlalchemy.ext.asyncio import AsyncSession

from database.database import get_db
from core.base.api_mode import ApiResponse, api_success
from core.pagination import DependsPagination, PageData

from modules.customer_info.services.customer_info_service import CustomerInfoService
from modules.customer_info.schemas.customer_info_schema import CustomerInfoCreate, CustomerInfoUpdate
from modules.customer_info.params.customer_info_params import CustomerInfoQuery
from modules.customer_info.vos.customer_info_vo import CustomerInfoResponse


router = APIRouter()


@router.post(
    "/list",
    response_model=ApiResponse[PageData[CustomerInfoResponse]],
    summary="获取客户信息表列表",
    dependencies=[DependsPagination],
)
async def list_customer_infos(query_params: CustomerInfoQuery, db: AsyncSession = Depends(get_db)):
    result = await CustomerInfoService.get_list_with_pagination(db, query_params)
    return api_success(data=result, msg="获取成功")


@router.get("/detail/{pk}", response_model=ApiResponse[CustomerInfoResponse], summary="获取客户信息表详情")
async def get_customer_info(pk: int, db: AsyncSession = Depends(get_db)):
    result = await CustomerInfoService.get_by_id(db, pk)
    return api_success(data=result, msg="获取成功")


@router.post("/create", response_model=ApiResponse[CustomerInfoResponse], status_code=status.HTTP_201_CREATED, summary="创建客户信息表")
async def create_customer_info(body: CustomerInfoCreate, db: AsyncSession = Depends(get_db)):
    result = await CustomerInfoService.create(db, body)
    return api_success(data=result, msg="创建成功")


@router.put("/update/{pk}", response_model=ApiResponse[CustomerInfoResponse], summary="更新客户信息表")
async def update_customer_info(pk: int, body: CustomerInfoUpdate, db: AsyncSession = Depends(get_db)):
    result = await CustomerInfoService.update(db, pk, body)
    return api_success(data=result, msg="更新成功")


@router.delete("/delete/{pk}", response_model=ApiResponse[dict], summary="删除客户信息表")
async def delete_customer_info(pk: int, db: AsyncSession = Depends(get_db)):
    ok = await CustomerInfoService.delete(db, pk)
    return api_success({"success": ok}, msg="删除成功" if ok else "未删除")
